package cn.itsource.controller;

import cn.itsource.domain.*;
import cn.itsource.service.IStudentService;
import cn.itsource.util.ExcelUtil;
import cn.itsource.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

//@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
    private static final Logger log = LoggerFactory.getLogger(TeacherController.class);
    @Autowired
    private IStudentService studentService;

    // 保存学生信息
    @PutMapping("/save")
    public JSONResult<Teacher> save(@RequestBody Student student) {
        System.out.println(student);
        try {
            studentService.save(student);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    // 更新学生信息
    @PostMapping("/update")
    public JSONResult<Teacher> update(@RequestBody Student student) {
        try {
            studentService.update(student);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    // 删除学生信息
    @DeleteMapping("/dlt/{id}")
    public JSONResult<Teacher> dlt(@PathVariable("id") Long id) {
        try {
            studentService.dlt(id);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    // 根据学生ID获取学生信息
    @GetMapping("/getStudent/{id}")
    public JSONResult<Student> get(@PathVariable("id") Long id) {
        log.info("{} getStudent 方法被调用了，参数为：{}", new Date(), id);

        try {
            Student student = studentService.getStudent(id);
            return new JSONResult<Student>().setData(student);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    // 获取所有学生信息
    @GetMapping("/getStudentAll")
    public JSONResult<Object> getAll() {
        log.info("{} getStudentAll 方法被调用了：", new Date());
        try {
            List<Student> all = studentService.getStudentAll();
            return JSONResult.ok().setData(all);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    // 批量插入学生信息
    @GetMapping("/batchInsert")
    public void download(HttpServletResponse response) throws IOException {
        ExcelUtil.exportExcel(studentService.getStudentAll(),"学生列表","学生",Student.class,"学生列表",response);
    }

    // 导入Excel文件
    @PostMapping("/importExcel")
    public JSONResult<Teacher> importExcel(MultipartFile file) throws IOException {
        try {
            List<Student> teachers = ExcelUtil.importExcel(file, 1, 1, Student.class);
            studentService.batchInsert(teachers);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail("导入失败，输入信息有误"+e.getMessage());
        }
    }

    // 导出学生信息到Excel文件
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtil.exportExcel(studentService.getStudentAll(),"学生列表","学生",Teacher.class,"学生列表",response);
    }
}